﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySQLStudio.BusinessLogic;
using MySQLStudio.GUI.Controls;

namespace MySQLStudio.GUI.Controls
{
    public partial class ViewForm: Telerik.WinControls.UI.RadForm
    {
        private enum ViewFormType
        {
            CREATE_VIEW,
            ALTER_VIEW
        }; 
        
        private String m_TableSchema = String.Empty;
        private String m_ViewName = "new_view";
        private String m_CreateViewTemplate = "CREATE VIEW OR REPLACE '{0}'.'{1}' AS";

        //    ALTER
        //    [ALGORITHM = {UNDEFINED | MERGE | TEMPTABLE}]
        //    [DEFINER = { user | CURRENT_USER }]
        //    [SQL SECURITY { DEFINER | INVOKER }]
        //    VIEW view_name [(column_list)]
        //    AS select_statement
        //    [WITH [CASCADED | LOCAL] CHECK OPTION]

        private String m_AlterViewTemplate1 = "ALTER ALGORITHM = {0} DEFINER = {1} SQL SECURITY {2} VIEW {3} AS {4}";
        private String m_AlterViewTemplate = "ALTER {0}";

        private ViewFormType m_ViewFormType = ViewFormType.CREATE_VIEW;
        private Host m_host;
        private Database m_database;
    
        public Database Database
        {
            get { return m_database; }
            set { m_database = value; }
        }
        public Host Host
        {
            get { return m_host; }
            set { m_host = value; }
        }

        public String TableSchema
        {
            get { return m_TableSchema; }
            set { m_TableSchema = value; }
        }

        public String ViewName
        {
            get { return m_ViewName; }
            set { m_ViewName = value; }
        }
        
        public ViewForm()
        {
            InitializeComponent();

            tbDDL.Seperators.Add(' ');
            tbDDL.Seperators.Add('\r');
            tbDDL.Seperators.Add('\n');
            tbDDL.Seperators.Add(',');
            tbDDL.Seperators.Add('.');
            tbDDL.Seperators.Add('-');
            tbDDL.Seperators.Add('+');
            //tbDDL.Seperators.Add('*');
            //tbDDL.Seperators.Add('/');


            /*tbDDL.HighlightDescriptors.Add(new HighlightDescriptor("<", Color.Gray, null, DescriptorType.Word, DescriptorRecognition.WholeWord, true));

            tbDDL.HighlightDescriptors.Add(new HighlightDescriptor("<<", ">>", Color.DarkGreen, null, DescriptorType.ToCloseToken, DescriptorRecognition.StartsWith, false));
*/
            tbDDL.HighlightDescriptors = Highlighters.Sql;
        }

        public void LoadNewViewTemplate()
        {
            m_ViewFormType = ViewFormType.CREATE_VIEW;
           
            tbName.Text = m_ViewName;
            tbDDL.Text = String.Format(m_CreateViewTemplate, m_TableSchema, m_ViewName);
            tbDDL.OnTextChanged();
        }
        
        public void LoadAlterView(String connectionString,String schemaName, String viewName)
        {
            m_ViewName = viewName;
            m_ViewFormType = ViewFormType.ALTER_VIEW;

            tbDDL.Text = FixAlterViewDBResponse(ViewController.GetAlterViewInDataBase(connectionString, schemaName, viewName));
            tbDDL.OnTextChanged();
        }

        private String FixAlterViewDBResponse(String resp)
        {
            int indx = resp.ToLower().IndexOf("create");
            int length = "create".Length;
            return String.Format(m_AlterViewTemplate, resp.Substring(indx + length));
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            ViewController.Execute(Host.ConnectionString, tbDDL.Text);
        }

        private void btnRevert_Click(object sender, EventArgs e)
        {

        }

        private void tbDDL_TextChanged(object sender, EventArgs e)
        {
           // Console.Out.WriteLine("BLA11111");
        }

        private void tbDDL_TextChanging(object sender, Telerik.WinControls.TextChangingEventArgs e)
        {
            //Console.Out.WriteLine("BLA");
        }

        private void ViewForm_Load(object sender, EventArgs e)
        {
 
	
		
        }
    }
}
